/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Predecessores.java
 *
 * Created on 11/11/2011, 22:34:16
 */
package praticav.view;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import praticav.dao.AtividadesPlanejadasDAO;
import praticav.dao.FasesDAO;
import praticav.dao.ProducaoDAO;
import praticav.model.AtividadesPlanejadas;
import praticav.model.AtividadesPlanejadasPK;
import praticav.model.Fases;
import praticav.model.FasesPK;
import praticav.model.Producao;

/**
 *
 * @author Jonatah Benatti
 */
public class Predecessores extends javax.swing.JFrame {

    int FasCodigo=0;
    int AtiCodigo=0;
    int ProCodigo=0;
    /** Creates new form Predecessores */
    public Predecessores(int ProCodigoP) {
        ProCodigo = ProCodigoP;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setTitle("Consulta Dependências");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Sequencial", "Etapa", "Inicio", "Fim", "Predecessor", "Antes", "LT", "Depois", "", ""
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTable1KeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(30);
        jTable1.getColumnModel().getColumn(2).setMaxWidth(100);
        jTable1.getColumnModel().getColumn(3).setMaxWidth(100);
        jTable1.getColumnModel().getColumn(4).setMaxWidth(50);
        jTable1.getColumnModel().getColumn(5).setMaxWidth(50);
        jTable1.getColumnModel().getColumn(6).setMaxWidth(50);
        jTable1.getColumnModel().getColumn(7).setMaxWidth(50);
        jTable1.getColumnModel().getColumn(8).setMaxWidth(20);
        jTable1.getColumnModel().getColumn(9).setMaxWidth(20);

        jButton1.setText("Alterar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Recalcular Datas");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Fechar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 849, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 600, Short.MAX_VALUE)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton3)
                    .addComponent(jButton2))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        
        carregaTabela();

    }//GEN-LAST:event_formWindowOpened

    private void jTable1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyPressed
        // TODO add your handling code here:

    }//GEN-LAST:event_jTable1KeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (AtiCodigo == 0){//Selecionou uma Fase
            FasesPK chave = new FasesPK();
            chave.setFascodigo(FasCodigo);
            Producao p = new ProducaoDAO().buscar(ProCodigo);            
            chave.setProducao(p);
            //JOptionPane.showMessageDialog(null, "chave.getFascodigo(): "+chave.getFascodigo()+"\nchave.getProducao().getProcodigo(): "+chave.getProducao().getProcodigo());
            Fases fas = new FasesDAO().buscar(chave);
            FasesView f = new FasesView(fas);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
        else{
            AtividadesPlanejadasPK chave = new AtividadesPlanejadasPK();
            chave.setAtiplanCodigo(AtiCodigo);
            FasesPK fasePK = new FasesPK();
            Producao p = new ProducaoDAO().buscar(ProCodigo);
            fasePK.setProducao(p);
            fasePK.setFascodigo(FasCodigo);
            //atPK.setProcodigo(ar.getChave().getProcodigo());
            //atPK.setFascodigo(ar.getChave().getFascodigo());
            Fases fase = new FasesDAO().buscar(fasePK);
            chave.setFase(fase);            
            //chave.setFascodigo(FasCodigo);
            //chave.setProcodigo(ProCodigo);
            AtividadesPlanejadas at = new AtividadesPlanejadasDAO().buscar(chave);
            AtividadesPlanejadasView ATv = new AtividadesPlanejadasView(at);
            ATv.setLocationRelativeTo(null);
            ATv.setVisible(true);
            
        }
        carregaTabela();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        Producao p = new ProducaoDAO().buscar(ProCodigo);
        ProducaoDAO dao = new ProducaoDAO();
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(new Date());
        try {
            dao.calculaInicio(p, 0, gc);
        } catch (ParseException ex) {
            Logger.getLogger(Predecessores.class.getName()).log(Level.SEVERE, null, ex);
        }
        carregaTabela();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        FasCodigo = Integer.parseInt(""+jTable1.getValueAt(jTable1.getSelectedRow(), 8));
        AtiCodigo = Integer.parseInt(""+jTable1.getValueAt(jTable1.getSelectedRow(), 9));
    }//GEN-LAST:event_jTable1MouseClicked

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        this.hide();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // TODO add your handling code here:
        carregaTabela();
    }//GEN-LAST:event_formWindowActivated

    public void carregaTabela(){
                //Prepara tabela
        DefaultTableModel modeloFase = (DefaultTableModel) jTable1.getModel();
        modeloFase.setNumRows(0);
        
        
        FasesDAO daoFase = new FasesDAO();
        //SequenciaAtividades
        //Navega nas fases da produção
        for (Fases f : daoFase.listar(ProCodigo, 0, "", "", "", "", "")) { 
            AtividadesPlanejadasDAO daoAT = new AtividadesPlanejadasDAO();
            
            SimpleDateFormat formatarDate = new SimpleDateFormat("dd-MM-yyyy");   
            String DataStrIni = formatarDate.format(f.getFasdtainicial());
            String DataStrFim = formatarDate.format(f.getFasdtafinal());
            modeloFase.addRow(new String[] {""+ f.getFassequencial(), ""+ f.getFasdescricao(),  "" + DataStrIni, ""+DataStrFim, "" + f.getFaspredecessor(), "", "", "", "" + f.getChave().getFascodigo(), "0"});            
            //navega nas atividades das fases
            for (AtividadesPlanejadas at : daoAT.listar(ProCodigo, f.getChave().getFascodigo(), 0, "", "", "", "", "")) { 
                DataStrIni = formatarDate.format(at.getAtiplandtainicial());
                DataStrFim = formatarDate.format(at.getAtiplandtafinal());
                modeloFase.addRow(new String[] {""+ at.getAtiplansequencial(), "           "+at.getAtiplandescricao(), ""+DataStrIni, ""+DataStrFim,"" + at.getAtiplanpredecessor(), ""+at.getAtiplanintervaloantes(), ""+at.getAtiplanleadtime(),""+at.getAtiplanintervalodepois(), ""+at.getChave().getFase().getChave().getFascodigo(), "" + at.getChave().getAtiplanCodigo()});            
            }       
        }
    }
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
